package com.whisperly.echotalk.other;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.LocaleList;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.whisperly.echotalk.bllmodel.ReturnedValue;
import com.whisperly.echotalk.starryb.R;
import com.whisperly.echotalk.starryb.netWorkUtils.NetworkCallback;
import com.whisperly.echotalk.starryb.netWorkUtils.NetworkUtils;
import com.whisperly.echotalk.starryb.utils.DatabaseHelper;
import com.whisperly.echotalk.starryb.utils.GlobalData;

import java.util.Locale;

public class FeedBack extends AppCompatActivity {
    private EditText feedback_content;
    private Spinner feedback_type;
    private ProgressBar progressBar;
    private Button feedback_submit;
    private Toast currentToast;
    @Override
    protected void attachBaseContext(Context newBase) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(newBase);
        String languageCode = prefs.getString("language", "en");
        super.attachBaseContext(updateResources(newBase, languageCode));
    }

    public Context updateResources(Context context, String languageCode) {
        Locale locale = new Locale(languageCode);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context = context.createConfigurationContext(config);
        return context;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback);

        feedback_type = findViewById(R.id.feedback_type);
        feedback_content = findViewById(R.id.content);
        feedback_submit = findViewById(R.id.feedback_submit);
        progressBar = findViewById(R.id.progressBar);

        feedback_submit.setOnClickListener(v -> submitFeedback());

    }

    private void submitFeedback() {

        String feedbackType = feedback_type.getSelectedItem().toString();
        String feedbackContent = feedback_content.getText().toString();

        if (GlobalData.getUsername().equals("")){
            if (currentToast != null) {
                currentToast.cancel();
            }

            currentToast = Toast.makeText(this, R.string.Please_log_in_first, Toast.LENGTH_SHORT);
            currentToast.show();
            return;
        }

        if (feedbackContent.isEmpty()) {
            if (currentToast != null) {
                currentToast.cancel();
            }

            currentToast = Toast.makeText(this, R.string.Please_enter_your_feedback, Toast.LENGTH_SHORT);
            currentToast.show();
            return;
        }
        if (feedbackType.isEmpty()) {
            if (currentToast != null) {
                currentToast.cancel();
            }

            currentToast = Toast.makeText(this, R.string.Please_select_a_feedback_type, Toast.LENGTH_SHORT);
            currentToast.show();
            return;
        }

        sendFeedbackToServer(GlobalData.getUsername(),feedbackType, feedbackContent);
        progressBar.setVisibility(View.VISIBLE);

        feedback_submit.setVisibility(View.GONE);
    }


    private void sendFeedbackToServer(String userName,String feedbackType, String feedbackContent) {
        NetworkUtils.postFeedback(userName,feedbackType, feedbackContent, new NetworkCallback() {
            @Override
            public void onSuccess(ReturnedValue response) {
                Log.e("123","onSuccess");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (currentToast != null) {
                            currentToast.cancel();
                        }
                        progressBar.setVisibility(View.GONE);
                        feedback_submit.setVisibility(View.VISIBLE);
                        currentToast = Toast.makeText(FeedBack.this, R.string.The_feedback_was_submitted_successfully, Toast.LENGTH_SHORT);
                        currentToast.show();
                    }
                });
            }

            @Override
            public void onFailure(Throwable throwable) {
                Log.e("123","onFailure");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try(DatabaseHelper dbHelper = new DatabaseHelper(FeedBack.this)){
                            boolean b = dbHelper.insertFeedback(feedbackType, feedbackContent);
                            if (currentToast != null) {
                                currentToast.cancel();
                            }
                            if (b){
                                currentToast = Toast.makeText(FeedBack.this, R.string.The_feedback_was_submitted_successfully, Toast.LENGTH_SHORT);
                            }else {
                                currentToast = Toast.makeText(FeedBack.this, R.string.Feedback_submission_failed, Toast.LENGTH_SHORT);
                            }
                            progressBar.setVisibility(View.GONE);
                            feedback_submit.setVisibility(View.VISIBLE);
                            currentToast.show();
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                });
            }
        });
    }
}
